<!-- home -->
<template>
  <div class="home">
    <div class="num">
      <el-card
        shadow="hover"
        :body-style="{ display: 'flex', padding: 0 }"
        v-for="item in countData"
        :key="item.name"
      >
        <div :style="{ background: item.color }" class="ticon">
          <component :is="item.icon" class="icons" />
        </div>
        <div class="detail">
          <p class="value">{{ item.value }}</p>
          <p class="name">{{ item.name }}</p>
        </div>
      </el-card>
    </div>

    <el-card shadow="hover" :body-style="{ display: 'flex' }">
      <line-chart :data="orderData" v-if="orderData" />
      <el-table :data="tableData" stripe style="width: 800px">
        <el-table-column prop="name" label="产品" width="100" />
        <el-table-column prop="todayBuy" label="今日" width="100" />
        <el-table-column prop="monthBuy" label="本月" width="100" />
        <el-table-column prop="totalBuy" label="总共" />
      </el-table>
    </el-card>
    <el-row :gutter="20">
      <el-col :span="12" style="margin-top: 20px">
        <el-card shadow="hover">
          <pie-chart :data="videoData" v-if="videoData" />
        </el-card>
      </el-col>
      <el-col :span="12" style="margin-top: 20px">
        <el-card shadow="hover">
          <bar-chart :data="userData" v-if="userData" />
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang ="ts">
import { onMounted, ref } from "vue";
import { api } from "@/utils/api";
import lineChart from "@/components/lineChart.vue";
import pieChart from "../components/pieChart.vue";
import barChart from "../components/barChart.vue";
const getImgUrl = (user : string) => {
  return new URL(`../assets/img/${user}.jpg`, import.meta.url).href;
};
interface CountItem {
  name: string;
  color: string;
  icon: any;
  value: number | string;
}
const tableData = ref([]);
const countData = ref<CountItem[]>([]);
const orderData = ref(null);
const videoData = ref(null);
const userData = ref(null);
const NETWORK_ERR = ref('网络错误');
const loadTable = async () => {
  try {
    let response;
    response = await api.get("/api/home/getTableData");
    // console.log('Table>' , response);
    let { code, data, msg } = response;
    if (code == 200) {
      tableData.value = data.tableData;
    } else {
      ElMessage.error(msg || NETWORK_ERR);
      return Promise.reject(msg || NETWORK_ERR);
    }
  } catch (error) {
    console.error("加载表格信息失败>", error);
    ElMessage.error("加载表格信息失败");
  }
};
const loadCount = async () => {
  try {
    let response;
    response = await api.get("/api/home/getCountData");
    let { code, data, msg } = response;
    if (code == 200) {
      countData.value = data;
    } else {
      ElMessage.error(msg || NETWORK_ERR);
      return Promise.reject(msg || NETWORK_ERR);
    }
  } catch (error) {
    console.error("加载Count信息失败>", error);
    ElMessage.error("加载Count信息失败");
  }
};
const loadChart = async () => {
  try {
    let response;
    response = await api.get("/api/home/getChartData");
    let { code, data, msg } = response;
    if (code == 200) {
      orderData.value = data.orderData;
      videoData.value = data.videoData;
      userData.value = data.userData;
    } else {
      ElMessage.error(msg || NETWORK_ERR);
      return Promise.reject(msg || NETWORK_ERR);
    }
  } catch (error) {
    console.error("加载chart信息失败>", error);
    ElMessage.error("加载chart信息失败");
  }
};
onMounted(() => {
  loadTable();
  loadCount();
  loadChart();
});
</script>

<style scoped lang="less">
.home {
  height: 100%;
  overflow: hidden;
  .table {
    margin-top: 20px;
  }
  .num {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    .el-card {
      width: 32%;
      margin-bottom: 20px;
    }
    .ticon {
      width: 80px;
      height: 80px;
      text-align: center;
    }
    .icons {
      margin-top: 25px;
      width: 30px;
      height: 30px;
      color: aliceblue;
    }
    .detail {
      margin-left: 20px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      .name {
        font-size: 13px;
        text-align: center;
        color: darkgrey;
      }
      .value {
        font-size: 30px;
        margin-bottom: 10px;
      }
    }
  }
}
</style>
